import 'package:flutter/material.dart';
import '../../../app_views.dart';

/// 万年历卡片组件 - Calendar card component
/// 
/// 显示万年历信息的卡片
/// Card component for displaying calendar information
class CalendarCard extends StatelessWidget {
  final TodayInfo? todayInfo;
  final VoidCallback? onTap;

  const CalendarCard({
    Key? key,
    this.todayInfo,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            AppColors.secondary.withOpacity(0.8),
            AppColors.accent.withOpacity(0.6),
          ],
        ),
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: AppColors.secondary.withOpacity(0.3),
            blurRadius: 12,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(16),
          child: Padding(
            padding: const EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题行 - Title row
                Row(
                  children: [
                    Icon(
                      Icons.calendar_today,
                      color: AppColors.textLight,
                      size: 24,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '万年历',
                      style: GoogleFonts.notoSansSc(
                        fontSize: 18,
                        fontWeight: FontWeight.w600,
                        color: AppColors.textLight,
                      ),
                    ),
                    const Spacer(),
                    Icon(
                      Icons.arrow_forward_ios,
                      color: AppColors.textLight.withOpacity(0.7),
                      size: 16,
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                
                if (todayInfo != null) ...[
                  // 日期信息 - Date information
                  _buildDateInfo(),
                  const SizedBox(height: 12),
                  
                  // 宜忌信息 - Suitable and avoid information
                  _buildSuitableAvoidInfo(),
                ] else ...[
                  // 加载状态 - Loading state
                  _buildLoadingState(),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 构建日期信息 - Build date information
  Widget _buildDateInfo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 公历日期 - Solar date
        Text(
          todayInfo!.solarDate,
          style: GoogleFonts.notoSansSc(
            fontSize: 20,
            fontWeight: FontWeight.w600,
            color: AppColors.textLight,
          ),
        ),
        const SizedBox(height: 4),
        
        // 农历日期 - Lunar date
        Row(
          children: [
            Text(
              todayInfo!.lunarDate,
              style: GoogleFonts.notoSansSc(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                color: AppColors.textLight.withOpacity(0.9),
              ),
            ),
            const SizedBox(width: 12),
            Text(
              todayInfo!.weekday,
              style: GoogleFonts.notoSansSc(
                fontSize: 14,
                color: AppColors.textLight.withOpacity(0.8),
              ),
            ),
          ],
        ),
        const SizedBox(height: 8),
        
        // 生肖和干支 - Chinese zodiac and Gan Zhi
        Row(
          children: [
            _buildInfoTag('${todayInfo!.chineseZodiac}年', Icons.pets),
            const SizedBox(width: 8),
            _buildInfoTag(todayInfo!.ganZhi, Icons.circle),
          ],
        ),
      ],
    );
  }

  /// 构建宜忌信息 - Build suitable and avoid information
  Widget _buildSuitableAvoidInfo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 宜 - Suitable
        if (todayInfo!.suitable.isNotEmpty) ...[
          _buildSuitableAvoidSection(
            '宜',
            todayInfo!.suitable,
            AppColors.success,
            Icons.check_circle_outline,
          ),
          const SizedBox(height: 8),
        ],
        
        // 忌 - Avoid
        if (todayInfo!.avoid.isNotEmpty) ...[
          _buildSuitableAvoidSection(
            '忌',
            todayInfo!.avoid,
            AppColors.error,
            Icons.cancel_outlined,
          ),
        ],
      ],
    );
  }

  /// 构建宜忌部分 - Build suitable/avoid section
  Widget _buildSuitableAvoidSection(
    String title,
    List<String> items,
    Color color,
    IconData icon,
  ) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: color.withOpacity(0.2),
            borderRadius: BorderRadius.circular(6),
            border: Border.all(
              color: color.withOpacity(0.5),
              width: 1,
            ),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Icon(
                icon,
                color: AppColors.textLight,
                size: 14,
              ),
              const SizedBox(width: 4),
              Text(
                title,
                style: GoogleFonts.notoSansSc(
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                  color: AppColors.textLight,
                ),
              ),
            ],
          ),
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            items.take(3).join('、'),
            style: GoogleFonts.notoSansSc(
              fontSize: 12,
              color: AppColors.textLight.withOpacity(0.8),
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }

  /// 构建信息标签 - Build information tag
  Widget _buildInfoTag(String text, IconData icon) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: AppColors.textLight.withOpacity(0.15),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            icon,
            color: AppColors.textLight.withOpacity(0.8),
            size: 14,
          ),
          const SizedBox(width: 4),
          Text(
            text,
            style: GoogleFonts.notoSansSc(
              fontSize: 12,
              fontWeight: FontWeight.w500,
              color: AppColors.textLight,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建加载状态 - Build loading state
  Widget _buildLoadingState() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 日期加载状态 - Date loading state
        Container(
          width: 150,
          height: 20,
          decoration: BoxDecoration(
            color: AppColors.textLight.withOpacity(0.3),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
        const SizedBox(height: 8),
        Container(
          width: 120,
          height: 16,
          decoration: BoxDecoration(
            color: AppColors.textLight.withOpacity(0.2),
            borderRadius: BorderRadius.circular(8),
          ),
        ),
        const SizedBox(height: 12),
        
        // 标签加载状态 - Tags loading state
        Row(
          children: [
            Container(
              width: 60,
              height: 24,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.2),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            const SizedBox(width: 8),
            Container(
              width: 80,
              height: 24,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.2),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
          ],
        ),
        const SizedBox(height: 12),
        
        // 宜忌加载状态 - Suitable/avoid loading state
        Row(
          children: [
            Container(
              width: 30,
              height: 20,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.2),
                borderRadius: BorderRadius.circular(6),
              ),
            ),
            const SizedBox(width: 8),
            Container(
              width: 100,
              height: 14,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.15),
                borderRadius: BorderRadius.circular(7),
              ),
            ),
          ],
        ),
      ],
    );
  }
}